package com.example.teamtribe.main.club.setup;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Utility;

import java.io.File;

public class SetUpPresenter implements SetUpContract.Presenter {

    private SetUpContract.View view;

    public SetUpPresenter(SetUpContract.View view) {
        this.view = view;
    }

    @Override
    public void setUp(String head, String name, String type, String comment) {
        if (TextUtils.isEmpty(head)) {
            view.showToast("请上传社团头像");
            return;
        }
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(comment)) {
            view.showToast("请填写完整的社团信息");
            return;
        }
        upload(head, name, type, comment);
    }

    private void upload(String head, final String name, final String type, final String comment) {
        view.showProgress();
        File file = new File(head);
        FileModel.getInstance().upload(Utility.getToken(), file, 0, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                setUp(name, type, comment);
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }

    private void setUp(String name, String type, String comment) {
        User user = Utility.getUser();
        new SetUpModel().setUp(Utility.getToken(), user.getSchoolId(), name, comment, user.getRealId(), new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {view.next(); }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }
}
